<template>
  <!-- 角色表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="内容编号" prop="contentCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.contentCode"
          placeholder="请输入内容编号"
          clearable
        />

        <span v-else>{{ formData.contentCode }}</span>
      </el-form-item>
      <el-form-item label="内容名称" prop="contentName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.contentName"
          placeholder="请输入内容名称"
          clearable
        />

        <span v-else>{{ formData.contentName }}</span>
      </el-form-item>
      <el-form-item label="打印模式" prop="printMode">
        <el-select
          v-if="!props.isReadonly"
          v-model="formData.printMode"
          placeholder="请选择打印模式"
          clearable
        >
          <el-option
            v-for="item in dictionaryStore.filterDictList('PrintMode')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <span v-else>{{
          dictionaryStore.filterDictName("PrintMode", formData.printMode)
        }}</span>
      </el-form-item>
      <el-form-item label="打印机名称" prop="printerName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.printerName"
          placeholder="请输入打印机名称"
          clearable
        />

        <span v-else>{{ formData.printerName }}</span>
      </el-form-item>
      <el-form-item label="打印纸张" prop="printPaper">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.printPaper"
          placeholder="请输入打印纸张"
          clearable
        />

        <span v-else>{{ formData.printPaper }}</span>
      </el-form-item>
      <el-form-item label="进纸方向" prop="pagerOrient">
        <el-select
          v-if="!props.isReadonly"
          v-model="formData.pagerOrient"
          placeholder="请选择进纸方向"
          clearable
        >
          <el-option
            v-for="item in dictionaryStore.filterDictList('PagerOrient')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <span v-else>{{
          dictionaryStore.filterDictName("PagerOrient", formData.pagerOrient)
        }}</span>
      </el-form-item>
      <el-form-item label="纸张宽度(0.1mm)" prop="paperWidth">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.paperWidth"
          placeholder="请输入纸张宽度(0.1mm)"
          clearable
        />

        <span v-else>{{ formData.paperWidth }}</span>
      </el-form-item>
      <el-form-item label="纸张高度(0.1mm)" prop="paperHeight">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.paperHeight"
          placeholder="请输入纸张高度(0.1mm)"
          clearable
        />

        <span v-else>{{ formData.paperHeight }}</span>
      </el-form-item>
      <el-form-item label="打印内容模板" prop="printContentTemplate">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.printContentTemplate"
          placeholder="请输入打印内容模板"
          clearable
        />

        <span v-else>{{ formData.printContentTemplate }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "SYSPRINTCONTENTSETTINGADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  contentCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  contentName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  printMode: [
    { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" },
  ],
  printerName: [
    { min: 1, max: 100, message: "长度在 1 到 100 个字符", trigger: "blur" },
  ],
  printPaper: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  pagerOrient: [
    { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" },
  ],
  paperWidth: [],
  paperHeight: [],
  printContentTemplate: [
    {
      min: 1,
      max: 65535,
      message: "长度在 1 到 65535 个字符",
      trigger: "blur",
    },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  contentCode: "",
  contentName: "",
  printMode: "",
  printerName: "",
  printPaper: "",
  pagerOrient: "",
  paperWidth: "",
  paperHeight: "",
  printContentTemplate: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        contentCode,
        contentName,
        printMode,
        printerName,
        printPaper,
        pagerOrient,
        paperWidth,
        paperHeight,
        printContentTemplate,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        contentCode,
        contentName,
        printMode,
        printerName,
        printPaper,
        pagerOrient,
        paperWidth,
        paperHeight,
        printContentTemplate,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.contentCode = "";
  formData.contentName = "";
  formData.printMode = "";
  formData.printerName = "";
  formData.printPaper = "";
  formData.pagerOrient = "";
  formData.paperWidth = "";
  formData.paperHeight = "";
  formData.printContentTemplate = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

/** 打印内容设置保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/system/sysprintcontentsetting/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

// 导入excel
const uploadApi = "/system/sysprintcontentsetting/upload";
const excelBox = ref(false);
const uploadBtn = ref(false);
const uploadFile = ref("");
const fileName = ref("");

function excelBoxOpen() {
  excelBox.value = true;
}

function getfilepath(e: any) {
  const file = e.target.files[0];
  fileName.value = file.name;
  uploadFile.value = file;
  uploadBtn.value = false;
}

function upload() {
  if (!uploadFile.value) {
    ElMessage({
      type: "warning",
      message: "请选择文件！",
    });
    return;
  }
  let formdata = new FormData();
  formdata.append("file", uploadFile.value);
  let config = { headers: { "Content-Type": "multipart/form-data" } };
  request
    .post(uploadApi, formdata, config)
    .then((res) => {
      console.log(ref());
    })
    .catch((error) => {
      console.log(error);
    });
}
</script>
